<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/22 0022
 * Time: 下午 16:53
 */
namespace  app\admin\controller;

use  \think\Db;
use think\Session;
class charge extends Permissions
{
    /**
     * 管理员列表
     * @return [type] [description]
     */
    public function index()
    {
        $res = Session::get("admin_cate_id");
        $ress = Session::get("admin");
        $resa = Db::name('admin')->where('id',$ress)->value('name');
        $resb = Db::name('agent')->where('phone',$resa)->value('id');

        $post = $this->request->param();
        if (isset($post['order_num']) and !empty($post['order_num'])) {
            $where['order_num'] = ['like', '%' . $post['order_num'] . '%'];
        }
         if (isset($post['phone']) and !empty($post['phone'])) {
            $where['phone'] = ['like', '%' . $post['phone'] . '%'];
        }
        $model = Db::name('charge');
        if($res==1){
            $charge = empty($where) ? $model->alias('a')->join('agent b','a.agent_id=b.id')
                      ->field('a.id,a.add_time,b.name,phone,recharge_money,charge_ratio,charge_money,order_num,a.last_money')->order('id desc')
                      ->paginate(20) : $model->alias('a')->join('agent b','a.agent_id=b.id')
                      ->field('a.id,a.add_time,b.name,phone,recharge_money,charge_ratio,charge_money,order_num,a.last_money')->where($where)->order('id desc')
                      ->paginate(20, false, ['query' => $this->request->param()]);
        }else{
            $charge = empty($where) ? $model->alias('a')->join('agent b','a.agent_id=b.id')
                      ->field('a.id,b.name,a.add_time,phone,recharge_money,charge_ratio,charge_money,order_num,a.last_money')->where('agent_id',$resb)->order('id desc')
                      ->paginate(20) : $model->alias('a')->join('agent b','a.agent_id=b.id')
                      ->field('a.id,a.add_time,b.name,phone,recharge_money,charge_ratio,charge_money,order_num,a.last_money')->where($where)->where('agent_id',$resb)
                      ->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        }
        
        $this->assign('charge', $charge);
        $info['cate'] = Db::name('charge')->where('agent_id')->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    /*
    *佣金列表的修改
    */
    public function delete()
    {
        $id = input('id');
        $info = Db::name('charge')->where('id',$id)->delete();
         if($info){
            return $this->success('删除成功','admin/charge/index');
         }else{
            return $this->error('删除失败');
         }
    }

    public function update()
    {
        $id = input('id');
        $data['recharge_money'] = input('rechargemoney');
        $data['charge_ratio'] = input('chargeratio');
        $data['charge_money'] = input('chargemoney');
        $data['last_money'] = input('lastmoney');
        $list = Db::name('charge')->where('id',$id)->update($data);
        if ($list){
            $this->success('修改成功','admin/charge/index');
        }else{
            $this->error('修改失败');
        }
    }

}